home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group93a.txt / 000111_icon-group-sender _Fri Apr 2 15:06:23 1993.msg < prev    next >
Internet Message Format  |  1993-04-21  |  3KB

  1. Received: by cheltenham.cs.arizona.edu; Sat, 10 Apr 1993 10:27:38 MST
  2. Date: 2 Apr 93 15:06:23 GMT
  3. From: uchinews!ellis!goer@speedy.wisc.edu  (Richard L. Goerwitz)
  4. Organization: University of Chicago
  5. Subject: Re: Icon rookie problems solved
  6. Message-Id: <1993Apr2.150623.19869@midway.uchicago.edu>
  7. References: <9364@kielo.uta.fi>
  8. Sender: icon-group-request@cs.arizona.edu
  9. To: icon-group@cs.arizona.edu
  10. Status: R
  11. Errors-To: icon-group-errors@cs.arizona.edu
  12.  
  13. In article <9364@kielo.uta.fi> jere@uta.fi (Jere K{pyaho) writes:
  14.  
  15. >For the record, here is my solution:
  16. >
  17. >procedure genchar( s, c )
  18. >  s ? {
  19. >    every i := upto( c ) do {
  20. >      tab( i )
  21. >      write( move( i ) )
  22. >    }
  23. >  }
  24. >end
  25. >
  26. >
  27. >Although I'm fascinated by Icon, I don't find this example
  28. >too intuitive.
  29.  
  30. You made the scanning expression the outside enclosing control
  31. structure, which is great.  Here's another general rule:  If you
  32. are using "every" within a scanning expression, you're doing some-
  33. thing wrong.  This rule isn't hard and fast, and you certainly
  34. shouldn't feel bad about using it above.  If we remove it and the
  35. intermediate variable, though, we get something a bit more elegant
  36. and (to most Icon programmers) more intuitive:
  37.  
  38. (1)
  39.     s ? {
  40.         while tab( upto(c) ) do
  41.             write( move(1) )
  42.     }
  43.  
  44. You may remember that the do-clause can be omitted, so you can do
  45. this more compactly:
  46.  
  47. (2)
  48.     s ? while write( (tab(upto(c)), move(1)) )
  49.  
  50. Note that (2) uses the (a,b) construct.  a & b might just as well
  51. have been used.  In either case, a must succeed and produce a re-
  52. sult before b will be tried.  What this does is make it so that,
  53. for every time tab(upto(c)) succeeds, move(1) will be executed,
  54. and the expression as a whole (either (a,b) or a & b) will produce
  55. the result of b, i.e. the result of move(1):
  56.  
  57. (2)
  58.     s ? while write( tab(upto(c)) & move(1) )
  59.  
  60. Both of these seem to me to be idiomatic Icon code, although it
  61. is true that opinions differ.  Some prefer (1).  Some prefer (3)
  62. to (2).  I don't like (2) because it looks messy :-).
  63.  
  64. The key to scanning is that scanning handles all of your place-
  65. keeping for you, so you don't have to use "every" or the like.
  66. When I tab( upto(c) ), then the first result produced by upto(c)
  67. is where I tab to, and that becomes my position (my &pos, in Icon's
  68. terms).  If I want the next c, I can just move(1) past the current
  69. c, and then do another tab( upto(c) ).  Icon knows where I am in
  70. the string, and I don't have to keep track of it myself.  The next
  71. time I tab( upto(c) ) I'll end up at the next match!
  72.  
  73. As a result of this, your
  74.  
  75.     every i := upto(c)
  76.  
  77. is a neat idea (i.e. produce all the positions at which c chars
  78. occur).  But it's almost too clever.  By simply doing a
  79.  
  80.     while tab( upto(c) ) do
  81.         write( move(1) )
  82.  
  83. you are moving up to successive positions at which c occurs, then
  84. move()ing past them and printing the character matched - and this
  85. without using an i variable or any every structures (which tend
  86. to be counterintuitive within scanning expressions).
  87.  
  88. Hope this helps.
  89.  
  90. Regarding 32-bit Icon:  Icon is really meant for CPUs and OSs
  91. that operate in a larger address space than DOS machines tradi-
  92. tionally offer.  Not that the DOS implementation is a bad one.
  93. It's amazingly clever, in fact.
  94.  
  95. -- 
  96.  
  97.    -Richard L. Goerwitz              goer%midway@uchicago.bitnet
  98.    goer@midway.uchicago.edu          rutgers!oddjob!ellis!goer
  99.